package ru.susu.algebra.centralunits.alternating.initializers;

import java.util.List;

import ru.susu.algebra.centralunits.CentralUnitsOptions;
import ru.susu.algebra.centralunits.alternating.MathMethodWithInitializers;
import ru.susu.algebra.chartable.constructor.AlternatingCharTableUtil;
import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.properties.IPropertySource;

/**
 * @author akargapolov
 * @since: 02.09.2010
 */
public class PartitionsInitializer extends MathMethodWithInitializers<Object>
{
	private static final String ASCENDING_ROW_PARTITIONS = "AscendingRowPartitions";
	private static final String NUMBER_ROWS = "NumberRows";

	@Override
	protected Object directRun(IPropertySource ps) throws Exception
	{
		int number = CentralUnitsOptions.getNumber(ps);
		if (ps.getValue(ASCENDING_ROW_PARTITIONS) == null)
			ps.setValue(ASCENDING_ROW_PARTITIONS, AlternatingCharTableUtil.getAscendingDegreeFirstAssociatedPartitions(number));

		if (ps.getValue(NUMBER_ROWS) == null)
		{
			List<Partition> rows = getRowPartitions(ps);
			Partition last = rows.get(rows.size() - 1);
			ps.setValue(NUMBER_ROWS, AlternatingCharTableUtil.indexRowNumbers(rows).get(last));
		}
		return null;
	}

	@Override
	protected Class[] getDependentInitializers()
	{
		return new Class[0];
	}

	public static List<Partition> getRowPartitions(IPropertySource ps)
	{
		return (List<Partition>)ps.getValue(ASCENDING_ROW_PARTITIONS);
	}

	public static Integer getNumberRows(IPropertySource ps)
	{
		return (Integer)ps.getValue(NUMBER_ROWS);
	}

}
